﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;

using Microsoft.TeamFoundation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TFSCommon.Settings
{
    public partial class MySettingsControl : Common.Controls.SettingsControl
    {
        public override string this[string key]
        {
            get
            {
                if (!String.IsNullOrEmpty(key))
                {
                    switch (key.ToUpperInvariant())
                    {
                        case MySettingsManager.ParameterTFSCollection:
                            return TFSCollection;
                        case MySettingsManager.ParameterTFSCollectionUri:
                            return TFSCollectionUri;
                        case MySettingsManager.ParameterTFSProject:
                            return TFSProject;
                    }
                }

                return null;
            }

            set
            {
                if (!String.IsNullOrEmpty(key))
                {
                    switch (key.ToUpperInvariant())
                    {
                        case MySettingsManager.ParameterTFSCollection:
                            TFSCollection = value;
                            break;
                        case MySettingsManager.ParameterTFSCollectionUri:
                            TFSCollectionUri = value;
                            break;
                        case MySettingsManager.ParameterTFSProject:
                            TFSProject = value;
                            break;
                    }
                }
            }
        }

        public String TFSCollection
        {
            get { return txtCollection.Text; }
            set { txtCollection.Text = value; }
        }

        public String TFSCollectionUri
        {
            get { return txtCollectionUri.Text; }
            set { txtCollectionUri.Text = value; }
        }

        public String TFSProject
        {
            get { return txtProject.Text; }
            set { txtProject.Text = value; }
        }

        public MySettingsControl()
        {
            InitializeComponent();
        }

        private void btnServers_Click(object sender, EventArgs e)
        {
            TeamProjectPicker picker = new TeamProjectPicker();

            if (DialogResult.OK == picker.ShowDialog())
            {
                TFSCollection = picker.SelectedTeamProjectCollection.Name;
                TFSCollectionUri = picker.SelectedTeamProjectCollection.Uri.AbsoluteUri;
                TFSProject = picker.SelectedProjects[0].Name;
            }
        }

        public void ResetTest()
        {
            foreach (Control c in grpTFS.Controls)
            {
                if (c is PictureBox)
                {
                    grpTFS.Controls.Remove(c);
                }
            }
        }

        public void Success(int step)
        {
            TestResult(step, Common.Resources.CommonResources.ImageTestSuccess);
        }

        public void Fail(int step)
        {
            TestResult(step, Common.Resources.CommonResources.ImageTestFail);
        }

        public void TestResult(int step, Bitmap bmp)
        {
            Point position = new Point();
            Control control = null;

            switch (step)
            {
                case 0:
                    control = txtCollection;
                    break;
                case 1:
                    control = txtCollectionUri;
                    break;
                case 2:
                    control = txtProject;
                    break;
            }

            if (null != control)
            {
                position = new Point(control.Right + 2, control.Top);

                PictureBox pb = new PictureBox();
                pb.Image = bmp;
                pb.Size = new System.Drawing.Size(20, 20);
                pb.Location = position;

                grpTFS.Controls.Add(pb);
            }
        }
    }
}
